Find the second most repeated wordΒΆ
Find the second most repeated word in a given string.
def word_count(S):
counts = dict()
words = S.split()
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
counts_x = sorted(counts.items(), key=lambda kv: kv[1])
# print(counts_x)
return counts_x[-2]
# test
print(word_count("Both of these issues are fixed by postponing \
the evaluation of annotations. Instead of compiling code which \
executes expressions in annotations at their definition time, \
the compiler stores the annotation in a string form equivalent \
to the AST of the expression in question. If needed, annotations \
can be resolved at runtime using typing.get_type_hints(). \
In the common case where this is not required, the annotations \
are cheaper to store (since short strings are interned by the \
interpreter) and make startup time faster."))
Output:
('of', 4)